﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    {
        [ChildActionOnly]
        public ActionResult MPBWZ04(string returnUrl)
        {
            ModelState.Clear(); 
            ViewBag.ReturnUrl = returnUrl;
            MWB.REMARK = INFO;
            MWB.INFO = GetWBInfo();
            return PartialView(MWB);
        }
        public string GetWBInfo()
        {
            string info = "";
            MWB.FOLGE = MWB.PCS; 
            foreach (KeyValuePair<string, string> kp in MWB.WHPCS)
            {
                string[] vu = kp.Value.Split('{');
                if (kp.Key != MWB.NEWTP) continue;
                //info += kp.Key + "批," + vu[0] + "卷," + vu[1].Replace("}", "") + "米;";
                MWB.PCS = int.Parse(vu[0]);
                MWB.QMTR = double.Parse(vu[1].Replace("}", ""));
                MWB.FOLGE = int.Parse(vu[0]);
            }
            info = (int.Parse(MWB.NEWTP) + "批," + MWB.PCS + "卷," + Helper.RoundX(MWB.QMTR, 1) + "米;").Replace(".", ".");
            return info;
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MPBWZ04(WBBSTModel model, string returnUrl)
        {

            try
            {
                StartNewJob();
                //WBW.POSTCHG();
            }
            catch (Exception ex)
            {
                //VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void StartNewJob()
        {
            MWB = new WBBSTModel(MWB);
            MWB.NEWUSER = CUSER;
            MWB.WHPCS.Clear();
            MWB.INFO = "";
            MABF.IsPDCReady = false;
            INFO = "请扫描布卷";
        }

        [ChildActionOnly]
        public ActionResult PBWZ04(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            return PartialView(MWB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult PBWZ04(WBBSTModel model, string returnUrl)
        {
            try
            {
                PB2WZ(model);//移库操做  
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                //ModelState.AddModelError("",ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void PB2WZ(WBBSTModel model)
        {
            if (model == null)
                return;
            if (model.WBBSTKNR == null)
                return;
            model.WBBSTKNR = model.WBBSTKNR.Trim().PadRight(12);
            dwData = VHP.VWBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBAUFTYP"] = "V";
            dwData["WBBAUFKRS"] = MWB.ABPAUF.Substring(0, 2);
            dwData["WBBAUFLFD"] = MWB.ABPAUF.Substring(2, 6);
            dwData["WBBAUFPOS"] = MWB.ABPAUF.Substring(8, 3);
            dwData["WBBSTKNR"] = model.WBBSTKNR.Trim().PadRight(12);
            DataSet SResult = VHP.VWBB.QWB24(dwData);
            if (SResult.Tables[0].Rows.Count == 0)
            {
                MWB.IsDataValid = false;
                INFO = "不在范围内";
                return;
            }
            MWB.IsDataValid = true;
            DataRow _dwWB = SResult.Tables[0].Copy().Rows[0];
            double mtr = Convert.ToDouble(_dwWB["WBBISTKLG"].ToString()) * 0.001;
            Helper.GetSTime(ref date, ref time, ref stime);
            _dwWB["WBBAUFTYP"] = "V";
            _dwWB["WBBAUFKRS"] = MWB.ABPAUF.Substring(0, 2);
            _dwWB["WBBAUFLFD"] = MWB.ABPAUF.Substring(2, 6);
            _dwWB["WBBAUFPOS"] = MWB.ABPAUF.Substring(8, 3);
            _dwWB["WBBTP"] = MWB.NEWTP;
            _dwWB["WBBFOLGE"] = Helper.Right("00" + (MWB.FOLGE + 1), 2);
            _dwWB["WBBGEBNR"] = MWB.GEBNR.PadRight(8);
            _dwWB["WBBAENDBEN"] = CUSER;
            _dwWB["WBBAENDDAT"] = date;
            PM["WBROW"] = _dwWB;
            PM["RUNID"] = RUNID;
            VHP.VWBB.WBB61(PM);//先留位，再转移
            VHP.VWBB.MOVE2PB(_dwWB, "01", "ZL", "FB      ", RUNID);

            INFO = "完成(" + model.WBBSTKNR.Trim() + "," + Helper.RoundX(mtr, 1) + "m)";
            Tracer.Info("扫描:", INFO);
            if (MWB.WHPCS.ContainsKey(MWB.NEWTP))
            {
                string[] vu = MWB.WHPCS[MWB.NEWTP].Split('{');
                MWB.PCS++;
                MWB.QMTR += mtr;
                MWB.WHPCS[MWB.NEWTP] = MWB.PCS + "{" + +MWB.QMTR + "}";
            }
            else
            {
                MWB.WHPCS[MWB.NEWTP] = "1{" + mtr + "}";
            }
        }
    }
}
